// pages/my/myInfo.js
//var card = require("../card/card.js")
Page({
  data:{
    
  },
  onLoad:function(options){
    var that = this;
    var open_id = options.openid;
    var wx_Name = options.wxName;
    that.setData({
      openid: open_id,
      wxName: wx_Name
    })
  },
  uploader:function(){
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          photos: tempFilePaths
        })
        
      }
    })
  }
  ,
  formSubmit: function (e){
    var user = e.detail.value
    
    var that = this;
    wx.request({
      url: 'http://localhost:8080/add',
      method: 'GET',
      data: {
        openId: that.data.openid,
        wxName: that.data.wxName,
        UserName: user.inputName,
        Job: user.inputJob,
        Phone: user.inputPhone,
        email: user.inputEmail,
        CNname: user.inputGongsiCn,
        USname: user.inputGongsiUs,
        Fax: user.inputFax,
        Address: user.inputAddress,
        UserIcon: that.data.photos[0]

      },
      
      success: function (res) {
        var userID = res.data.myCard;
        if (userID == null || userID == "文件类型为空" || userID == "只支持图片格式") {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: '创建名片失败,请检查信息',
            icon: 'none',
            duration: 2000
          })

        } else {
          that.setData({
            UserName: '',
            Job: '',
            Phone: '',
            email: '',
            CNname: '',
            USname: '',
            Fax: '',
            Address: '',
            UserIcon: ''
             })  
               var openId = wx.getStorageSync('useronlyData').openid;   
               wx.showToast({
                          title:  '添加成功',
                          icon:  'success',
                          duration:  2000
                    })
              wx.redirectTo({
                url: '../my/myInfo?openid=' + openId,
          })

         
        }
      }
    }),
      wx.uploadFile({
      url: 'http://localhost:8080/upLoad/uploadUserIcon',      //此处换上你的接口地址
        filePath: that.data.photos[0],
        name: 'file',
        header: {
          "Content-Type": "multipart/form-data",
          'accept': 'application/json'

        },
        formData: {

        },
        success: function (res) {
          var data = res.data;
          console.log('data');
        },
        fail: function (res) {
          console.log('fail');

        },
      })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})